package com.jiang.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jiang.dao.BaseDAOHiberTemp;
import com.jiang.dao.INewsDao;
import com.jiang.vo.News;

@Repository
public class NewsDaoImpl extends BaseDAOHiberTemp implements INewsDao {

	
	@Override
	public Boolean doCreate(News news) throws SQLException {
		// 验证的部分都在控制层完成，这里就是只持久化，无业务
		Integer id = (Integer) super.getHibernateTemplate().save(news);
		return id != null;
	}

	@Override
	public Boolean doUpdate(News news) throws SQLException {
		// 这里不推荐直接session.update() 这样的话就只更新有值的没有值的数据就null了
		// 要解决上面的问题两种方案(1) 先查询再比较修改(几乎不可能这种方法) (2) 直接使用HQL或SQL
		String hql = "UPDATE News SET title=?, pubdate=?,content=?,price=? WHERE nid=?";
		Query query = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, news.getTitle()!=null? news.getTitle():"");
		query.setParameter(1, news.getPubdate()!=null?news.getPubdate():null);
		query.setParameter(2, news.getContent()!=null?news.getContent():"");
		query.setParameter(3, news.getPrice()!=null?news.getPrice():null);
		query.setParameter(4, news.getNid());
		return query.executeUpdate()>0;
	}

	@Override
	public Boolean doRemoveBatch(Set<Integer> ids) throws SQLException {
		// 使用HQL拼IN
		String hql = "DELETE FROM News WHERE nid IN (";
		StringBuffer sb = new StringBuffer(hql);
		for(Integer id: ids){
			sb.append(id+",");
		}
		// 删除最后的逗号
		sb.delete(sb.length()-1, sb.length());
		sb.append(")");
		Query query = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sb.toString());
		return query.executeUpdate()>0;
	}

	@Override
	public News findById(Integer id) throws SQLException {
		
		Criteria criteria = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(News.class);
		Criterion criterion = Restrictions.eq("nid", id);
		criteria.add(criterion);
		News news = (News) criteria.uniqueResult();
		return news;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<News> findAll() throws SQLException {
		
		Criteria criteria = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(News.class);
		List news = criteria.list();
		return news;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> findAllSplit(String column, String keyWord, Integer currentPage, Integer lineSize)
			throws SQLException {
		
		String hql = " FROM News WHERE " + column + " LIKE ? ";
		Query query = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setParameter(0, "%"+keyWord+"%");
		query.setFirstResult((currentPage-1)*lineSize);
		query.setMaxResults(lineSize);
		List<News> news = query.list();
		return news;
	}

	@Override
	public Long getAllCount(String column, String keyWord) throws SQLException {
		
		String hql = "SELECT COUNT(nid) FROM News WHERE "+ column + " LIKE ? ";
		Query query = super.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setParameter(0, "%"+keyWord+"%");
		Long num = (Long)query.uniqueResult();
		return num;
	}

}
